package com.gc;


import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;

public class HttpUtils {
	private static int timeout = 30000; // http超时时间
	private static Charset utf8 = Charset.forName("UTF-8");
	
	public static String doGet(String requestUrl) throws IOException {
		//BaseBean bb = new BaseBean();
		
        BufferedReader inReader = null;
        InputStream in = null;
        String responseBody = "";
        try {
            URL url = new URL(requestUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(timeout);
            conn.setReadTimeout(timeout);
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Content-Type", "application/json");
//            conn.setDoInput(true);

            in = conn.getInputStream();
            inReader = new BufferedReader(new InputStreamReader(in, utf8));

            StringBuilder sb = new StringBuilder();
            int len;
            char[] tmp = new char[256];
            while ((len = inReader.read(tmp)) > 0) {
                sb.append(tmp, 0, len);
            }
            responseBody = sb.toString();
            conn.disconnect();
            return responseBody;
        } catch (MalformedURLException e) {
            e.printStackTrace();
         //   bb.writeLog("MalformedURLException：" + e.getMessage());
            responseBody = "ERR";
        } catch (IOException e) {
            e.printStackTrace();
           // bb.writeLog("IOException：" + e.getMessage());
            responseBody = "ERR";
        } finally {
            if (inReader != null) {
                inReader.close();
            }
            if (in != null) {
                in.close();
            }

        }
        return responseBody;
    }

	public static void main(String[] args) throws IOException {
		String requestUrl = "http://127.0.0.1/api/lmf/GetEmployeeInfoList";
		String responseBody = doGet(requestUrl);
		System.out.println(responseBody);
		System.exit(0);
	}
}
